package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysBookRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysBookMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysBook;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysBookService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 图书业务层处理
 *
 * @author caiJin
 * @date 2024-04-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysBookServiceImpl implements ISysBookService {

    private final SysBookMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysBook add(SysBookRequest request) {
        SysBook entity = SysBook.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysBookRequest request) {
        SysBook entity = SysBook.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysBook getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysBook> pageList(SysBookRequest request) {
        Page<SysBook> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysBook> query = this.buildWrapper(request);
        IPage<SysBook> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysBook> list(SysBookRequest request) {
        LambdaQueryWrapper<SysBook> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysBook> buildWrapper(SysBookRequest param) {
        LambdaQueryWrapper<SysBook> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysBook::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getAuthor())) {
            query.like(SysBook::getAuthor, param.getAuthor());
        }
        if (param.getPicture() != null) {
            query.like(SysBook::getPicture, param.getPicture());
        }
        if (param.getClassId() != null) {
            query.like(SysBook::getClassId, param.getClassId());
        }
        return query;
    }

}
